package chat;

import java.io.IOException;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Vector;

public class SocketManager extends Vector
{
	SocketManager()
	{
		
	}
	void add(Socket socket)
	{
		super.add(socket);
	}
	void remove(Socket socket)
	{
		super.remove(socket);
	}
	synchronized void sendMessage(String message)
	{
		PrintWriter writer=null;
		Socket socket;
		for(int i=0;i<size();i++)
		{
			socket=(Socket)elementAt(i);
			try
			{
				writer=new PrintWriter(socket.getOutputStream(),true);
				
			}
			catch(IOException e)
			{
				System.err.println("发生异常:"+e);
		        e.printStackTrace();
			}
			if(writer!=null)
			writer.println(message);
		}
	}
	synchronized void sendClientNum()
	{
		String clientNum="当前在线人数为"+size();
		System.out.println(clientNum);
		sendMessage(clientNum);
		
	}
}